package com.bjbn.example;

import com.bjbn.entity.EqptMapping;
import com.bjbn.util.EqptMappingCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 资产映射缓存使用示例
 * 展示如何在业务代码中使用EqptMappingCacheUtil
 */
@Slf4j
@Component
public class EqptMappingCacheUsageExample {
    
    /**
     * 示例：根据设备编码获取资产信息
     */
    public void exampleGetByEqptCode() {
        String eqptCode = "EQPT001";
        EqptMapping mapping = EqptMappingCacheUtil.getByEqptCode(eqptCode);
        
        if (mapping != null) {
            log.info("找到设备映射: 设备编码={}, 资产编码={}, 资产类型={}", 
                    mapping.getEqptCode(), mapping.getAssetCode(), mapping.getAssetType());
        } else {
            log.warn("未找到设备编码为 {} 的映射", eqptCode);
        }
    }
    
    /**
     * 示例：根据BMS编码获取资产信息
     */
    public void exampleGetByBmsCode() {
        String bmsCode = "BMS001";
        EqptMapping mapping = EqptMappingCacheUtil.getByBmsCode(bmsCode);
        
        if (mapping != null) {
            log.info("找到BMS映射: BMS编码={}, 资产编码={}, 设备编码={}", 
                    mapping.getBmsCode(), mapping.getAssetCode(), mapping.getEqptCode());
        } else {
            log.warn("未找到BMS编码为 {} 的映射", bmsCode);
        }
    }
    
    /**
     * 示例：根据资产类型获取所有相关映射
     */
    public void exampleGetByAssetType() {
        String assetType = "空调设备";
        List<EqptMapping> mappings = EqptMappingCacheUtil.getByAssetType(assetType);
        
        if (mappings != null && !mappings.isEmpty()) {
            log.info("找到 {} 个 {} 类型的资产映射", mappings.size(), assetType);
            for (EqptMapping mapping : mappings) {
                log.info("  - 资产编码: {}, 设备编码: {}, BMS编码: {}", 
                        mapping.getAssetCode(), mapping.getEqptCode(), mapping.getBmsCode());
            }
        } else {
            log.warn("未找到 {} 类型的资产映射", assetType);
        }
    }
    
    /**
     * 示例：获取缓存统计信息
     */
    public void exampleGetCacheStats() {
        Map<String, Object> stats = EqptMappingCacheUtil.getCacheStats();
        if (stats != null) {
            log.info("缓存统计信息:");
            log.info("  - 总映射数量: {}", stats.get("totalMappings"));
            log.info("  - 缓存命中次数: {}", stats.get("cacheHitCount"));
            log.info("  - 缓存未命中次数: {}", stats.get("cacheMissCount"));
            log.info("  - 缓存命中率: {}", stats.get("hitRate"));
            log.info("  - 最后刷新时间: {}", stats.get("lastRefreshTime"));
            log.info("  - 刷新次数: {}", stats.get("refreshCount"));
        }
    }
    
    /**
     * 示例：检查缓存状态
     */
    public void exampleCheckCacheStatus() {
        boolean initialized = EqptMappingCacheUtil.isCacheInitialized();
        log.info("缓存是否已初始化: {}", initialized);
        
        if (initialized) {
            List<EqptMapping> allMappings = EqptMappingCacheUtil.getAllMappings();
            log.info("缓存中共有 {} 条资产映射记录", allMappings != null ? allMappings.size() : 0);
        }
    }
    
    /**
     * 示例：手动刷新缓存
     */
    public void exampleRefreshCache() {
        log.info("开始手动刷新缓存...");
        EqptMappingCacheUtil.refreshCache();
        log.info("缓存刷新完成");
    }
}
